"""2. 两数相加"""
from typing import Optional

from com.siyuan.ListNode import ListNode


class Solution:

    """双链表循环"""
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        if (not l2) and (not l1):
            return None
        res1 = ListNode()
        res2 = res1
        jw = 0
        while l1 or l2 or jw > 0:
            if l1:
                jw += l1.val
                l1 = l1.next if l1.next else None
            if l2:
                jw += l2.val
                l2 = l2.next if l2.next else None
            res2.next = ListNode(jw % 10)
            res2 = res2.next
            # 此处注意，python中jw/10返回的是小数
            jw = int(jw / 10)
        return res1.next


if __name__ == '__main__':
    l1 = ListNode(8)
    l2 = ListNode(5)
    sl = Solution()
    res = sl.addTwoNumbers(l1, l2)
    while res:
        print(res.val)
        res = res.next